import java.util.LinkedList;
import java.util.Queue;

public class MyStack {
    //利用两个队列来实现栈
    private Queue<Integer> queue1;//队列1
    private Queue<Integer> queue2;//队列2

    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if (queue1.isEmpty()) {
            //1为空
            queue1.offer(x);
        } else if (queue2.isEmpty()) {
            //2为空
            queue2.offer(x);
        }else{
            //两个都为空
            queue1.offer(x);
        }
    }

    public int pop() {
        if (empty()) {
            return -1;
        }
        if (!queue1.isEmpty()) {
            int size = queue1.size();
            for (int i = 0; i < size - 1; i++) {
                queue2.offer(queue1.poll());//将1弹出的值压入到2中
            }
            return queue1.poll();
        }else {
            int size = queue2.size();
            for (int i = 0; i < size - 1; i++) {
                queue1.offer(queue2.poll());//将1弹出的值压入到2中
            }
            return queue2.poll();
        }
    }

    //获取栈顶元素
    public int top() {
        if (empty()) {
            return -1;
        }
        if (!queue1.isEmpty()) {
            int size = queue1.size();
            int ret = -1;
            for (int i = 0; i < size - 1; i++) {
                ret = queue1.poll();
                queue2.offer(ret);//将1弹出的值压入到2中
            }
            return ret;
        }else {
            int size = queue2.size();
            int ret = -1;
            for (int i = 0; i < size - 1; i++) {
                ret = queue2.poll();
                queue1.offer(ret);//将1弹出的值压入到2中
            }
            return ret;
        }
    }

    //两个队列都为空就是栈为空
    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}
